<template>
	<view class="container">
		<u-alert-tips type="warning" :show="showTips" show-icon="true" @close="closeTips" close-able="true" description="请注意保护员工隐私信息!"></u-alert-tips>
		<template v-if="dataLists.length > 0">
			<view v-for="(item,index) in dataLists" :key="index" class=" u-margin-top-20 z-b-r white-bg u-padding-20 u-margin-bottom-20">
				<u-collapse>
					<u-collapse-item :title="item.name">
						<view class="u-margin-bottom-10">
							<text>性别：</text>
							<text>{{ item.sex }}</text>
						</view>
						<view class="u-margin-bottom-10">
							<text>联系方式：</text>
							<text>{{ item.phone }}</text>
						</view>
						<view class="u-margin-bottom-10">
							<text>住址：</text>
							<text>{{ item.address }}{{ item.detailAddre }}</text>
						</view>
						<view class="u-margin-bottom-10">
							<text>身份证号码：</text>
							<text>{{ item.idCard | decodeHex }}</text>
						</view>
						<view class="u-margin-bottom-10">
							<text>银行卡号：</text>
							<text>{{ item.bankID | decodeHex }}</text>
						</view>
						<view class="u-margin-bottom-10">
							<view class="u-flex u-row-between">
								<text>工资结算方式：{{ item.salaryWay || '' }}</text>
								<u-button type="primary" size="mini" ripple="true" @click="salaryWayShowEvent(item,index)">修改结算方式</u-button>
							</view>
							<u-select v-model="salaryWayShow" mode="single-column" @confirm="salaryWayConfirm" :list="salaryWays"></u-select>
						</view>
					</u-collapse-item>
				</u-collapse>
			</view>
		</template>
		<template v-else>
			<empty-data tipText="暂无员工信息"></empty-data>
		</template>
		<!-- 顶部提示 -->
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {mapState} from "vuex";
	import {encrypt} from "../../common/encrypt.js"
	export default {
		data() {
			return {
				salaryWayShow:false,
				showTips:true,
				projectID:"",
				workerIDs:[],
				dataLists:[],
				curWorkerData:{},
				curWorkerIndex:-1,
				salaryWays:[
					{
						value: 'c1',
						label: '按尺结算'
					},
					{
						value: 't2',
						label: '按天数结算'
					}
				]
			};
		},
		onLoad(params) {
			this.projectID = params.projectID;
			this.initDataList()
		},
		computed:{
			...mapState({
				token:"token"
			})
		},
		filters:{
			decodeHex(d){
				return encrypt.decodeHex(d);
			}
		},
		methods:{
			salaryWayShowEvent(item,index){
				this.curWorkerData = item;
				this.curWorkerIndex = index;
				this.salaryWayShow = true;
			},
			async salaryWayConfirm(e){
				let {token,projectID,curWorkerData,curWorkerIndex} = this;
				let data = {
					token,
					id:projectID,
					type:"worker",
					wkID:curWorkerData.wkID,
					salary_way:e[0].label
				}
				this.$loading("正在修改");
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/updateProjectSalaryWay",
						data
					}
				})
				
				uni.hideLoading()
				if(res.status === 0){
					this.dataLists[curWorkerIndex].salaryWay = e[0].label;
					this.$refs.uTips.show({
						title: '修改成功',
						type: 'success',
					})
				} else {
					this.$refs.uTips.show({
						title: '稍后重试',
						type: 'warning',
					})
				}
			},
			closeTips(){
				this.showTips = false;
			},
			async initDataList(){
				let {token,projectID} = this;
				let getWKInfoTask = [];
				// 获取员工ID
				this.$loading("正在加载")
				let wkIDRes = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/getProjectWKID",
						data:{token,projectID}
					}
				})
				
				if(wkIDRes.status === 0){
					this.workerIDs = wkIDRes.data[0].worker_ids;
					this.workerIDs.forEach(async item=>{
						getWKInfoTask.push(this.$network({
							api:"main",
							data:{
								action:"projectAdmin/getWorkerInfo",
								data:{wkID:item.wkID,token}
							}
						}))
					})
					
					let res = await Promise.all(getWKInfoTask);
					
					uni.hideLoading()
					res.forEach(item=>{
						if(item.status === 0){
							for (let i = 0; i < this.workerIDs.length; i++) {
								if(item.data[0].wkID == this.workerIDs[i].wkID){
									item.data[0].salaryWay = this.workerIDs[i].salaryWay
								}
							}
							this.dataLists.push(item.data[0])
						}
					})
				}
			}
		}
		
	}
</script>

<style lang="scss">

</style>
